//
//  GRBargainCodeView.m
//  YC_GRice
//
//  Created by luojinrong11 on 2019/4/17.
//  Copyright © 2019 YeCai. All rights reserved.
//

#import "GRBargainCodeView.h"
#import <CoreImage/CoreImage.h>
@implementation GRBargainCodeView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(void)awakeFromNib{
    [super awakeFromNib];
    [self.codeIamgeView.layer setBorderWidth:0.5];
    [self.codeIamgeView.layer setBorderColor:[HEXCOLOR(0xf8f8f8) CGColor]];
}

+(instancetype)sharecodeView{
    static  GRBargainCodeView * codeView = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        codeView = [GRBargainCodeView xx_instantiateFromNib];
    });
    return codeView;
}
-(void)showBargainCode:(NSString *)code QRCodeUrL:(NSString *)qrcodeUrl{
    self.titleLabel.text = @"优惠券";
    self.topOffset.constant = 48;
    self.bargainCodeLabel.hidden = NO;
    self.tipView.hidden = YES;
    self.bargainCodeLabel.text = code;
    [self.codeIamgeView sd_setImageWithURL:[NSURL URLWithString:qrcodeUrl] placeholderImage:[UIImage imageNamed:@""]];
    [self show];
}

- (IBAction)confrimAction:(id)sender {
    [self removeFromSuperview];
    
}

-(void)show{
    UIWindow * keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(keyWindow);
    }];
    
}

-(void)showshareCodeUrl:(NSString *)qrcodeUrl{
    self.titleLabel.text = @"扫码帮好友砍价";
    self.topOffset.constant = 0;
    self.bargainCodeLabel.hidden = YES;
    self.tipView.hidden = NO;
//    //创建名为"CIQRCodeGenerator"的CIFilter
//    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
//    //将filter所有属性设置为默认值
//    [filter setDefaults];
//
//    //将所需尽心转为UTF8的数据，并设置给filter
//    NSData *data = [qrcodeUrl
//                    dataUsingEncoding:NSUTF8StringEncoding];
//    [filter setValue:data forKey:@"inputMessage"];
//
//    //设置二维码的纠错水平，越高纠错水平越高，可以污损的范围越大
//    /*
//     * L: 7%
//     * M: 15%
//     * Q: 25%
//     * H: 30%
//     */
//    [filter setValue:@"H" forKey:@"inputCorrectionLevel"];
//
//    //拿到二维码图片，此时的图片不是很清晰，需要二次加工
//    CIImage *outPutImage = [filter outputImage];
        [self.codeIamgeView sd_setImageWithURL:[NSURL URLWithString:qrcodeUrl] placeholderImage:[UIImage imageNamed:@""]];
   [ self show];
    
}

- (UIImage *)sencond_getHDImgWithCIImage:(CIImage *)img size:(CGSize)size {
    
    //二维码的颜色
    UIColor *pointColor = [UIColor blackColor];
    //背景颜色
    UIColor *backgroundColor = [UIColor whiteColor];
    CIFilter *colorFilter = [CIFilter filterWithName:@"CIFalseColor"
                                       keysAndValues:
                             @"inputImage", img,
                             @"inputColor0", [CIColor colorWithCGColor:pointColor.CGColor],
                             @"inputColor1", [CIColor colorWithCGColor:backgroundColor.CGColor],
                             nil];
    
    CIImage *qrImage = colorFilter.outputImage;
    
    //绘制
    CGImageRef cgImage = [[CIContext contextWithOptions:nil] createCGImage:qrImage fromRect:qrImage.extent];
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetInterpolationQuality(context, kCGInterpolationNone);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage);
    UIImage *codeImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    CGImageRelease(cgImage);
    
    return codeImage;
}
@end
